using System;
using System.IO;
using System.Threading;
using AbstractClass.Net;
using NUnit.Framework;

namespace QuasarTest.Core
{
    [TestFixture]
    public class HttpPostTest
    {
        private HttpPostRequest _request;
        private static readonly ManualResetEvent ResetEvent = new ManualResetEvent(false);
        private static readonly string HttpPostMessage =
            "olpd=69F855178AF3C2C4192782D594C3F06F|__|b26143a1-53b4-4309-9662-09e5ee14caf9|__|" +
            "1084587679,1649852336,1843090439|__|1109220031,852248799,580605666,1121684201,332798134," +
            "1234610965,790773142,185208710;" +
            Environment.NewLine +
            "StateWebSite=expandedMSO_PageHashCode=7144719780&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" +
            "%2FwEPDwUBMGRkcl7dYh%2B40hAtf81tcF46W%2BCqSYM%3D&__EVENTVALIDATION=%2FwEWBwL" +
            "%2F4OH0AwKRsf2GBQKRyOuwCQKVmoi%2FBQKsqY6jDQLwqZ7pDAKs69rPDd91JhLT%2FH8Hu%2BtWPebZtrycJ47Z&ctl00" +
            "%24IWS_WH_CPH_Content%24ContactUsControl1%24firstName={0}&ctl00" +
            "%24IWS_WH_CPH_Content%24ContactUsControl1%24lastName={1}&ctl00" +
            "%24IWS_WH_CPH_Content%24ContactUsControl1%24Address={2}&ctl00" +
            "%24IWS_WH_CPH_Content%24ContactUsControl1%24PhoneNumber={3}&ctl00" +
            "%24IWS_WH_CPH_Content%24ContactUsControl1%24Msg={4}&ctl00" +
            "%24IWS_WH_CPH_Content%24ContactUsControl1%24Submit=Submit";
        
        [SetUp]
        public void Init()
        {
            _request = new HttpPostRequest("http://abstractclass.org/Register.aspx");
        }

        [Test]
        public void TestPost()
        {
            var postMessage = string.Format(HttpPostMessage, "HttpPostRequest", "NUnit Test", "nunittest@gmail.com", "1234", DateTime.Now);
            _request.PostData = postMessage;
            var response = _request.Post();
            var stream = response.GetResponseStream();
            if (stream == null) return;
            var reader = new StreamReader(stream);
            var data = reader.ReadToEnd().Trim();
            Assert.NotNull(data);
            Assert.IsTrue(data.Contains("#ThankYou"));
            Console.WriteLine(data);
            response.Close();
            reader.Close();
        }

        [Test]
        public void TestPostData()
        {
            var postMessage = string.Format(HttpPostMessage, "HttpPostRequest", "NUnit Test", "nunittest@gmail.com", "1234", DateTime.Now);
            _request.PostData = postMessage;
            var data = _request.PostAndGetData();
            Assert.NotNull(data);
            Assert.IsTrue(data.Contains("#ThankYou"));
            Console.WriteLine(data);
        }

        [Test]
        public void TestAsyncPost()
        {
            var postMessage = string.Format(HttpPostMessage, "HttpPostRequest", "NUnit Test", "nunittest@gmail.com", "1234", DateTime.Now);
            _request.PostData = postMessage;
            _request.BeginPost(RespCallBack, _request);
            ResetEvent.WaitOne();
        }

        private static void RespCallBack(IAsyncResult result)
        {
            var req = (HttpPostRequest) result.AsyncState;
            var resp = req.EndPost(result);
            var data = resp.GetResponseStream();
            if (data != null)
            {
                var reader = new StreamReader(data);
                var serverData = reader.ReadToEnd();
                Assert.NotNull(serverData);
                Assert.IsTrue(serverData.Contains("#ThankYou"));
                reader.Close();
                Console.Write(serverData);
            }
            ResetEvent.Set();
        }
    }
}